<?php
namespace app\crm\model;
use think\Model;
use think\Db;
use think\Cache;
use think\Loader;

class Visittask extends Model
{
	/**
     * 获取回访任务列表
     * @return array <回访任务列表信息>
     */
    public function getVisitTaskData()
    {
		$page = $_REQUEST["page"];
		$limit = $_REQUEST["limit"];
		$where = " 1 ";
        $where .= empty($_REQUEST["createuser"])?"":" AND createuser = '".$_REQUEST["createuser"]."'";
        $where .= " AND username = '".$_SESSION['user_info']["username"]."'";
		$where .= empty($_REQUEST["start_createtime"])?"":" AND createtime >= '".$_REQUEST["start_createtime"]."'";
        $where .= empty($_REQUEST["end_createtime"])?"":" AND createtime <= '".$_REQUEST["end_createtime"]."'";
        
        $where .= $_REQUEST["task_status"]=='Y'?" AND endtime > '".date("Y-m-d H:i:s")."'":"";
		$where .= $_REQUEST["task_status"]=='N'?" AND endtime <= '".date("Y-m-d H:i:s")."'":"";
        $count = Db::name('vip_task')->where($where)->count();
        // 查询状态为1的用户数据 并且每页显示10条数据
        $dataList = Db::name('vip_task')->where($where)->limit(($page-1)*$limit,$limit)->order("createtime desc")->select();
		$task = Cache::get('task');
		$saleProgram = Cache::get('saleProgram');
		$users = Cache::get('users');
		foreach($dataList as &$val){
			$val['createuser'] = $users[$val['createuser']]['cn_name'];
			$val['username'] = $users[$val['username']]['cn_name'];
			$val["task_type"] = $task[$val["task_type"]]["task_type"];
			$val["sale_program"] = $saleProgram[$val["sale_program"]]["program_name"];
			if($val["endtime"] > date("Y-m-d H:i:s")){
				$val["status"] = "<span class='layui-badge'>进行中</span>";
			}else{
				$val["status"] = "<span class='layui-badge layui-bg-gray'>已过期</span>";
			}
		}
        $rowsList = count($dataList) ? $dataList : false;
        $arrCate["code"] = 0;
        $arrCate["msg"] = '';
        $arrCate["count"] = $count;
        $arrCate["data"] = $rowsList;
        return $arrCate;
    }
	
	/**
     * 获取回访任务内客户资料
     * @return array <回访任务信息>
     */
    public function customerDataList()
    {
		$menuname = "Visit Task List";
		$p_menuname = $_SESSION['menu'][$menuname]; //父菜单
		$priv = $_SESSION["user_priv"][$p_menuname][$menuname];	
		$username = $_SESSION["user_info"]["username"];
		
		$page = $_REQUEST["page"];
		$limit = $_REQUEST["limit"];
		$task_id = $_REQUEST["task_id"];
		
		$where = "(1 ";
        $where .= "AND b.task_id='$task_id'";
        $where .= empty($_REQUEST["name"])?"":" AND name like '%".$_REQUEST["name"]."%'";
        $where .= empty($_REQUEST["customer_code"])?"":" AND b.customer_code like '%".$_REQUEST["customer_code"]."%'";
        $where .= empty($_REQUEST["phone"])?"":" AND phone like '%".$_REQUEST["phone"]."%'";
        $where .= empty($_REQUEST["start_createtime"])?"":" AND createtime > '".$_REQUEST["start_createtime"]."'";
        $where .= empty($_REQUEST["end_createtime"])?"":" AND createtime < '".$_REQUEST["end_createtime"]."'";
        $where .= " )";
		
		$count = Db::name('vip_customer_basic')->alias('b')->join('xy_vip_customer_detail d','b.customer_code = d.customer_code')->field('b.*, d.member_level, d.sex, d.age, d.last_order_time, d.last_call_time, d.integral, d.id_card, d.order_num, d.sign_num')->where($where)->count();
        // 查询状态为1的用户数据 并且每页显示10条数据
        $customerList = Db::name('vip_customer_basic')->alias('b')->join('xy_vip_customer_detail d','b.customer_code = d.customer_code')->field('b.*, d.member_level, d.sex, d.age, d.last_order_time, d.last_call_time, d.integral, d.id_card, d.order_num, d.sign_num')->where($where)->limit(($page-1)*$limit,$limit)->order('b.createtime DESC')->select();
		$users = Cache::get('users');
		foreach($customerList as &$val){
			$val['create_name'] = $users[$val['create_user']]['cn_name'];
			switch ($val['sex']) {
                case '1': $val['sex'] = '男'; break;
                case '2': $val['sex'] = '女'; break;
            }
            switch ($val['member_level']) {
                case '1': $val['member_level'] = 'a类客户'; break;
                case '2': $val['member_level'] = 'b类客户'; break;
                case '3': $val['member_level'] = 'c类客户'; break;
                case '4': $val['member_level'] = 'd类客户'; break;
                case '5': $val['member_level'] = 'e类客户'; break;
            }
            switch ($val['customer_type']) {
                case 'first': $val['customer_type'] = '首单客户'; break;
                case 'second': $val['customer_type'] = '升级客户'; break;
                case 'vip': $val['customer_type'] = '会员客户'; break;

            }
			
			//判断号码是否加密
			if($priv["encrypt_phone"] == "Y"){
				if($username != "admin"){
					$val["phone"] = substr($val["phone"],0,3)."*****".substr($val["phone"],-2);
					$val["telephone"] = substr($val["telephone"],0,3)."*****".substr($val["telephone"],-2);
				}
			}
		}
		
        $rowsList = count($customerList) ? $customerList : false;
        $arrCate["code"] = 0;
        $arrCate["msg"] = '';
        $arrCate["count"] = $count;
        $arrCate["data"] = $rowsList;
        return $arrCate;
    }
	
	/**
     * 添加任务回访记录
     */
	public function inserTasktVisit($data)
	{
		$create_user = $_SESSION["user_info"]["username"];
		// 先根据客户资料id查到客户电话和任务id
        $res = Db::name('vip_customer_basic')->field('phone,task_id')->where("customer_code='".$data['customer_code']."'")->find();
        $data['visit_phone'] = $res['phone'];
		unset($data['enable_save']);
        // 新增
        $result = Db::name('vip_visit_record')->insert($data);
        if ($result){
            // 添加成功 更新最后回访时间
            $cus = Db::name('vip_customer_detail')->where("customer_code = '".$data['customer_code']."'")->update(['last_call_time' => date("Y-m-d H:i:s")]);
            if($_REQUEST["enable_save"] == "Y"){
				//把资料转交给自己
				$res = Loader::model('Customer')->transferCustomer("customer_code = '".$data['customer_code']."'", $create_user);
			}else{
				//把资料放入回收池
			}
			echo json_encode(array('success'=>true,'msg'=>'保存成功！'));
        } else {
            echo json_encode(array('msg'=>'保存失败！'));
        }
	}
	
}

?>

